package com.freesky.biz.icbc.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.freesky.biz.icbc.service.IcbcLifeService;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/icbc/api")
public class IcbcApiController extends BaseController {

    @Autowired
    private IcbcLifeService icbcLifeService;

    @PreAuthorize("@ss.hasPermi('icbc:api:custmIdentify')")
    @GetMapping("/custmIdentify")
    public AjaxResult wjyxCustmIdentifyQry(String mobile,String idno) {
        return icbcLifeService.wjyxCustmIdentifyQry(mobile,idno);
    }

    @PreAuthorize("@ss.hasPermi('icbc:api:custmIdentify')")
    @PostMapping("/equityQry")
    public AjaxResult equityQry(@RequestBody String content) {
        JSONObject jsonObject = JSON.parseObject(content);
        String type = jsonObject.getString("type");
        if("custmIdentify".equalsIgnoreCase(type)){
            //客户编号
            String userId = jsonObject.getString("userId");
            //活动编号
            String activityId = jsonObject.getString("activityId");
            //活动日期
            String activityDate = jsonObject.getString("activityDate");
            //月份
            String month = jsonObject.getString("month");
            //手机号
            String mobileNo = jsonObject.getString("mobileNo");
            return icbcLifeService.wjyxEquityQry(userId,activityId,activityDate,month,mobileNo);
        } else if("challenge".equalsIgnoreCase(type)
                || "oilgift".equalsIgnoreCase(type) || "friday".equalsIgnoreCase(type)){
            //M5挑战赛
            Map<String, Object> respMap = new HashMap<>();
            String CUSTOM_ID = "";

            if("challenge".equalsIgnoreCase(type)){
                CUSTOM_ID = "202509041127059588406";
            } else if ("oilgift".equalsIgnoreCase(type)){       //爱购达标有礼
                CUSTOM_ID = "202506201420296161318,202506201420398216463,202506201419534991094,202506201420088587809";
            } else if("friday".equalsIgnoreCase( type)){  //月享优品
                CUSTOM_ID = "202508050923181738653";
            }
            String userId = jsonObject.getString("userId");
            String activityId = jsonObject.getString("activityId");
            if(StringUtils.isNotEmpty(activityId)){
                CUSTOM_ID = activityId;
            }
            //respMap.put("活动编号：",CUSTOM_ID);

            respMap.put("客户编号：",userId);
            String arrayActivityNo[] = CUSTOM_ID.split(",");

            StringBuilder result = new StringBuilder();
            for(int i=0;i<arrayActivityNo.length;i++){
                String activityNo = arrayActivityNo[i];
                result.append("【编号:" + activityNo +"】,【结果:");
                AjaxResult apiService = icbcLifeService.customDsfApiService(activityNo,userId,"2");
                if(apiService.isSuccess()){
                    if("0".equalsIgnoreCase(apiService.get(AjaxResult.DATA_TAG).toString())){
                        result.append("符合条件】");
                    } else {
                        result.append("不符合条件】");
                    }
                } else {
                    result.append("失败:"+apiService.getMsg()+"】");
                }
            }
            respMap.put("查询结果：",result);
            return AjaxResult.success(respMap);
        }
        return AjaxResult.error("类型错误");
    }

}
